﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using Wpf.Ui.Controls;
using Wpf.Ui;
using System.Configuration;
using XC.WpfHost.Views.Pages;
using System.Windows.Controls.Primitives;
using System;
using System.Collections.Generic;
using HZY.Framework.EntityFrameworkRepositories.Repositories;
using XC.Services.Admin.Framework;
using Microsoft.Extensions.DependencyInjection;
using XC.WpfHost.Views;
using XC.Managers.Accounts;
using XC.Models.DTO.Framework;
using System.Windows.Documents;
using System.Linq;
using NPOI.POIFS.Properties;

namespace XC.WpfHost.ViewModels
{
    public partial class MainWindowViewModel : ObservableObject
    {
        private bool _isInitialized = false;

        [ObservableProperty]
        private string _applicationTitle = String.Empty;

        [ObservableProperty]
        private ObservableCollection<object> _menuItems = new();


        [ObservableProperty]
        private ICollection<object> _footerMenuItems = new ObservableCollection<object>()
        {
        new NavigationViewItem("Settings", SymbolRegular.Settings24, typeof(SettingsPage))
    };

        [ObservableProperty]
        private ObservableCollection<MenuItem> _trayMenuItems =
            new()
            {
            new MenuItem { Header = "Home", Tag = "tray_home" },
            new MenuItem { Header = "Close", Tag = "tray_close" },
            };

        private readonly SysUserService _sysUserService;
        private readonly IAccountManager accountManager;
        public MainWindowViewModel(INavigationService navigationService, SysUserService sysUserService, IAccountManager accountManager)
        {
            this._sysUserService = sysUserService;
            this.accountManager = accountManager;
            if (!_isInitialized)
                InitializeViewModel();

        }

        private  async void InitializeViewModel()
        {
            var acountInfo = await _sysUserService.GetUserInfoAsync();
            ApplicationTitle = "WPF 后台权限管理系统";
            ObservableCollection<object> _menuItems = new()
            {
                new NavigationViewItem("驾驶舱", SymbolRegular.Color24, typeof(Home))
            };
            foreach (var menus in acountInfo.Menus)
            {
                // 为每个菜单项创建一个 NavigationViewItem
                NavigationViewItem viewItem = new NavigationViewItem
                {
                    Content = menus.Name,
                    Icon = new SymbolIcon { Symbol = (SymbolRegular)Enum.Parse(typeof(SymbolRegular), menus.Icon, true) }
                };

                // 如果菜单有子菜单，则递归处理子菜单
                AddMenuChildren(menus.Children, viewItem);
                _menuItems.Add(viewItem);
            }
            MenuItems = _menuItems;

            _isInitialized = true;
        }
        private void AddMenuChildren(IEnumerable<SysMenuTreeDto> menus, NavigationViewItem curMenuItem)
        {
            List<NavigationViewItem> childList = new List<NavigationViewItem>();
            foreach (var childMenu in menus)
            {
                // 创建子菜单项
               
                // 如果子菜单项有进一步的子菜单，递归调用
                if (childMenu.Children != null && childMenu.Children.Any())
                {
                    NavigationViewItem childViewItem = new NavigationViewItem
                    {
                        Content = childMenu.Name,
                       // Icon = new SymbolIcon { Symbol = (SymbolRegular)Enum.Parse(typeof(SymbolRegular), childMenu.Icon, true) }
                    };
                    childList.Add(childViewItem);
                    AddMenuChildren(childMenu.Children, childViewItem);  // 递归处理更深层次的子菜单
                }
                else
                {
                    NavigationViewItem childViewItem = new NavigationViewItem(childMenu.Name, GetCtrType("DashboardPage"));
                    childList.Add(childViewItem);
                }

            }
            // 将子菜单项加入子菜单列表
            curMenuItem.MenuItemsSource = childList.ToArray();

        }

        private Type GetCtrType(string name)
        {
            //需要指定控件所在的命名空间
            var type = Type.GetType("XC.WpfHost.Views.Pages." + name);
            return type;
        }


    }

}
